package com.mod.reanimator 
{
	import com.calefk.core.EntitySimple;
	import com.calefk.core.List;
	import com.calefk.core.Project;
	import com.calefk.util.helper;
	import com.calefk.util.Math2;
	import com.mod.Global;
	import flash.display.MovieClip;
	
	/**
	 * ...
	 * @author Adrian
	 */
	public class BaseVillager extends EntitySimple 
	{
		protected var mLife:int = 100;
		protected var mTotalLife:int = 100;
		
		private var mIsDie:Boolean;
		
		public var currentState:int;
		protected var lastState:int;
		
		protected var speed:Number = 0.15;
		public var collision:MovieClip;
		protected var myBar:BarVillager;
		
		public static const DEATH:int = 1;
		public static const MOVING:int = 2;
		public static const REVIVING:int = 3;
		public static const EXPLODING:int = 4;
		public static const FIGHTING:int = 5;
		public static const ENTER_BASE:int =6;
		
		public static const WIDTH_HEALTHBAR:int = 40;
		
		//HealthBar
		protected var healthBar:MovieClip;
		
		public function BaseVillager(canvas:MovieClip, nameClip:String=null, px:Number=0, py:Number=0, project:Project=null) 
		{
			super(canvas, nameClip, px, py, project);
			mLife = mTotalLife;
			
			healthBar = Global.project.getMovie("barGood");
			clip.addChild(healthBar); healthBar.y = -clip.height / 2; healthBar.x = -clip.width / 2 + 15;
			healthBar.visible = false;
			//--
			mClip.endReviving = endReviving;
			mClip.rotation = helper.randomInterval( -180, 180);
			currentState = BaseVillager.DEATH;
			collision = mClip.collision_mc;
			
			myBar = new BarVillager(canvas, "bar", 50, 20,project,this);
			myBar.clip.visible = false;
		}
		
		public function activateMyBar():void
		{
			myBar.clip.visible = true;
			trace("entro a activateMybar");
		}
		
		public function updateBar(dt:int):void 
		{
			myBar.update(dt);
		}
		
		public function changeBarState():void 
		{
			switchState(myBar.actualVillagerState);
		}
		
		public function switchState(newState:int):void
		{
			lastState = currentState;
			currentState = newState;
		}
		
		private function endReviving():void 
		{
			switchState(BaseVillager.MOVING);
			mClip.rotation = 0;
			if (healthBar) healthBar.visible = true;
		}
		
		public function deactivateMyBar():void 
		{
			myBar.clip.visible = false;
		}
		
		public function isDead():Boolean 
		{
			return currentState == BaseVillager.DEATH;
		}
		
		// DIE STUFF
		public function onReceiveDamage(mount:int):void {
			if (mIsDie) return;
			mLife += mount;
			healthBar.filler_mc.x = ( (mLife/mTotalLife) * WIDTH_HEALTHBAR );
			if (mLife <= 0) {
				mIsDie = true;
				onAnimationDie();
			}
		}
		private function onAnimationDie():void {
			//mClip.gotoAndStop("exploding");
			Global.playSound("snd_death", 1, 1);
			if (healthBar) healthBar.visible = false;
			currentState = EXPLODING;
		}
		private function endDie():void {
			toClean = true;
		}
		
	}

}